package com.youcai.springboot_day03.controller;

import com.youcai.springboot_day03.dao.DepartmentDaoImpl;
import com.youcai.springboot_day03.dao.EmployeeDaoImpl;
import com.youcai.springboot_day03.entity.Department;
import com.youcai.springboot_day03.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;


@Controller
public class empController {

    @Autowired
    private EmployeeDaoImpl employeeDao;
    @Autowired
    private DepartmentDaoImpl departmentDao;

    @RequestMapping("/emp/finall")
    public String empfinall(Model model){
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emp",employees);
        return "emp/list";
    }

    @RequestMapping("/emp/sava")
    public String empsava(Model model){
        Collection<Department> department = departmentDao.getDepartment();
        model.addAttribute("department",department);
        return "emp/sava";
    }

    @RequestMapping("/sava")
    public String empsavas(Employee employee){
        System.out.println(employee);
        employeeDao.savaEmployee(employee);
        return "redirect:/emp/finall";
    }


    //删除
   @RequestMapping("/delete/{eid}")
    public String empdelete(@PathVariable("eid") Integer eid){
        employeeDao.deleteEmployee(eid);
        return "redirect:/emp/finall";
    }

    //修改
    @RequestMapping("/emp/{eid}")
    public String empupdate(@PathVariable("eid")Integer eid,Model model){
        Collection<Department> department = departmentDao.getDepartment();
        model.addAttribute("department",department);

        Employee employee = employeeDao.employeeByEid(eid);
        model.addAttribute("employee",employee);
        return "emp/update";
    }
    @RequestMapping("/update")
    public String update(Employee employee){
        employeeDao.savaEmployee(employee);
        return "redirect:/emp/finall";
    }

}
